<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PriDeviceInfo extends Model
{
    use \App\Models\Traits\Company; 
    use \App\Models\Traits\ScopeCompanySite;
	use \App\Models\Traits\HasDevice;
    
    protected $table = 't_prideviceinfo';
    
    protected $primaryKey = 'pdi_index';
    
    protected $fillable = [
        'pdi_name', 'pdi_code', 'dpt_id', 'Co_ID', 'AreaId', 'pdi_updatetime', 
        'code_url', 'code_png', 'DragX', 'DragY', 'SubType',
        'pdi_local_en', 'pdi_uart_id', 'pdi_uart_pro', 'pdi_uart_dev_addr'
    ];
    
    protected $casts = [
        'Co_ID' => 'integer',
        'AreaId' => 'string',
        'dpt_id' => 'integer',
        'DragX' => 'integer',
        'DragY' => 'integer',
        'SubType'=>'integer',
        'pdi_local_en' => 'string',
        //'pdi_uart_id' => 'string',
    ];
    
    public $timestamps = false;
    
    public function company() {
        return $this->hasOne('\App\Models\Company', 'Co_ID', 'Co_ID');
    }
    
    public function area() {
        return $this->hasOne('\App\Models\Area', 'AreaId', 'AreaId');
    }
    
    public function types() {
        return $this->hasOne('\App\Models\DeviceType', 'dt_typeid', 'dpt_id');
    }
   
    public function deviceStatus() {
        return $this->hasOne('\App\Models\Realstatus', 'pdi_index', 'pdi_index');
    }
    
    public function realStatus() {
        return $this->hasOne('\App\Models\WaterAff', 'pdi_index', 'pdi_index');
    }
    
    public function getSiteField() {
        return 'AreaId';
    }
    
    //水务设备实时数据
    public function wateraff() {
        return $this->hasOne('\App\Models\WaterAff', 'pdi_index', 'pdi_index');
    }

    //实时告警
    public function realWarn() {
        return $this->hasMany('\App\Models\Phpwarn', 'pdi_index', 'pdi_index');
    }

    //dpt
    public function scopeDptId($query, $dptId) {
        if($dptId) {
            $query->where('dpt_id', $dptId);
        }
        return $query;
    }
}
